package com.desire.redis;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.util.Collections;
import java.util.List;

@SpringBootTest
public class Redis_Lua {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Test
    void test() {
        String cursor = "0";
        String count = "100";
        String lua = "return redis.call('scan',KEYS[1],'MATCH',ARGV[1],'count',ARGV[2])";
        RedisScript<List> script = RedisScript.of(lua, List.class);
        RedisSerializer redisSerializer = redisTemplate.getStringSerializer();
        do {
            List execute = redisTemplate.execute(script, redisSerializer, redisSerializer, Collections.singletonList(cursor), "b*", count);
            cursor = (String) execute.get(0);
            System.out.println(cursor);
            List<String> value1 = (List<String>) execute.get(1);
            System.out.println(value1);
        }
        while (!"0".equals(cursor));
    }
}
